import { Injectable } from '@nestjs/common';
import { NacosNamingClientService } from 'libs/nacos/nacosNamingClient.service';
import actionBuilder from 'libs/common/utils/actionBuilder';

@Injectable()
export default class AuthApi {
  private action = actionBuilder({
    timeout: 30 * 1000
  });
  constructor(
    private nacosNamingClientService: NacosNamingClientService
  ) { }

  private async getAuthOneHostPath() {
    const host = await this.nacosNamingClientService.selectOneInstances({
      serviceName: 'auth',
    })
    const path = 'http://' + host.ip + ':' + host.port
    return path
  }
  async validateIdentity(data: {
    token: string,
    service: string,
    api: string
  }): Promise<object> {
    const path = await this.getAuthOneHostPath()

    return await this.action({
      method: 'POST',
      url: '/auth/validateIdentity',
      data: data,
      baseURL: path
    });

  }
}
